#ifndef _DAG_APPLY_LINK_H_
#define _DAG_APPLY_LINK_H_

#include "dag.h"
#include <set>

namespace dag {
	template <typename _integral, bool _forward>
	void apply_sub_link(const _integral* src, 
						const Link& link,  
						_integral* dst) {
		std::set<size_t>::const_iterator iter;
		
		if (_forward) {
			iter = link.forwardSet().begin();
			while (iter != link.forwardSet().end()) {
				dst[link.forward(*iter)] = src[*iter];
				iter++;
			}
		}
		else {
			iter = link.reverseSet().begin();
			while (iter != link.reverseSet().end()) {
				dst[link.reverse(*iter)] = src[*iter];
				iter++;
			}
		}	
	}
	
	template<bool _forward>
	void apply_link(const Frame& sendFrame, 
					const FrameLink& link, 
					Frame& receiveFrame) {
		apply_sub_link<bool, _forward>(sendFrame.bools, 
									   link.bLink, 
									   receiveFrame.bools);
		apply_sub_link<int, _forward>(sendFrame.ints, 
									  link.iLink,
									  receiveFrame.ints);
		apply_sub_link<float, _forward>(sendFrame.floats, 
										link.fLink,
										receiveFrame.floats);
		apply_sub_link<double, _forward>(sendFrame.doubles,
										 link.dLink,
										 receiveFrame.doubles);		
	}
}

#endif